package com.ktjy.t382.ch02.controller;

import com.ktjy.t382.ch02.entity.SysUser;
import com.ktjy.t382.ch02.entity.vo.R;
import com.ktjy.t382.ch02.service.SysService;
import com.ktjy.t382.ch02.util.RandomStringGenerator;
import com.ktjy.t382.ch02.util.RedisUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class SysUserController {
    @Resource
    private SysService service;

    @RequestMapping("/login")
    public R select(@RequestBody SysUser user) {
        RandomStringGenerator randomStringGenerator = new RandomStringGenerator();
        String token = randomStringGenerator.generateRandomString(8);
        RedisUtil redisUtil = new RedisUtil();
        redisUtil.setKey("token", token);
        List<SysUser> login = service.login(user);
        return R.ok(1,"登录成功",login,token);
    }

    @RequestMapping("/balance")
    public R balance(String cardno) {
        return R.ok(service.getBalance(cardno));
    }

    @RequestMapping("/insert")
    public Map<String,Object> insert(String name1, String name2, BigDecimal money) {
//        Map<String,Object> map = new HashMap<>();
        Map<String, Object> balance = service.balance(name1, name2, money);
        return balance;
    }

    @RequestMapping("/update")
    public int update(@RequestBody SysUser user) {
        return service.update(user);
    }
}
